﻿@{
    Layout = "~/Views/Shared/_SysPage.cshtml";
    string controller = ViewBag.Controller;
    int type = ViewBag.Type;
    string typeName = ViewBag.TypeName;
    string defId = ViewBag.DefaultId;
}

@inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment HostingEnvironment
@section header{
    @BundlerHelper.Render(HostingEnvironment.ContentRootPath, Url.Content("~/lib/bootstrap.treetable/1.0/bootstrap-treetable.min.css"))
    @BundlerHelper.Render(HostingEnvironment.ContentRootPath, Url.Content("~/lib/bootstrap.treetable/1.0/bootstrap-treetable.min.js"))
}

<div class="container-div">
    <div class="row">
        <div id="searchDiv" class="col-sm-12 search-collapse">
            <form>
                <div class="select-list">
                    <ul>
                        <li>
                            <input id="type" col="Type" type="text" hidden="hidden" />
                        </li>
                        <li>
                            类别：<input id="typeName" col="TypeName" type="text" readonly="readonly" />
                        </li>
                        <li>
                            状态：<span id="status" col="Status"></span>
                        </li>
                        <li>
                            名称或描述：<input id="text" col="Text" type="text" />
                        </li>

                        <li><a class="btn btn-primary btn-sm" onclick="searchTreeGrid()"><i class="fa fa-search"></i>&nbsp;搜索</a></li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a id="btnAddTop" class="btn btn-success" onclick="showAddTopDialog()"><i class="fa fa-plus"></i> 创建</a>
            <a id="btnAdd" class="btn btn-success" onclick="showAddEditDialog(true)"><i class="fa fa-plus"></i> 新增</a>
            <a id="btnEdit" class="btn btn-primary" onclick="showAddEditDialog(false)"><i class="fa fa-edit"></i> 修改</a>
            <a id="btnDelete" class="btn btn-danger" onclick="deleteSelected()"><i class="fa fa-remove"></i> 删除</a>
            <a id="btnExpandAll" class="btn btn-info"><i class="fa fa-exchange"></i> 展开/折叠</a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="gridTable" data-mobile-responsive="true"></table>
        </div>
    </div>
</div>

<script type="text/javascript">
    var id = '';
    var type = '@type';
    var typeName = '@typeName';

    $(function () {
        $('#typeName').val('@typeName');
        $('#type').val('@type');
        $("#status").ysComboBox({ data: ys.getJson(@Html.Raw(typeof(StatusType).EnumToDictionaryJson())) });
        initTreeGrid();

    });

    function initTreeGrid() {
        var options = {
            code: "Id",
            parentCode: "ParentId",

            expandAll: false,
            expandFirst: false,
            toolbar: '#toolbar',
            expandColumn: '1',
            url: '@Url.Content("~/Scc/"+controller+"/GetManageDtosJson")'+'?type=@type',
            modalName: typeName,
            columns: [
                { field: 'selectItem', radio: true },
                { field: 'Name', title: '名称',
                    formatter: function (value, row, index) {
                        if (ys.isNullOrEmpty(row.Icon)) {
                            return row.Name;
                        } else {
                            return '<i class="' + row.Icon + '"></i> <span class="nav-label">' + row.Name + '</span>';
                        }
                    }
                },
                { field: 'Description', title: '描述'},
                { field: 'Sequence', title: '排序码', width: '60px'},
                {
                    field: 'Status', title: '状态', align: "left", width: '60px',
                    formatter: function (value, row, index) {
                        if (row.Status == @((int)StatusType.Enabled)) {

                            return '<span class="badge badge-primary">' + "@StatusType.Enabled.GetDescription()" + '</span>';
                        } else {
                            return '<span class="badge badge-warning">' + "@StatusType.Disabled.GetDescription()" + '</span>';
                        }
                    }
                },
                { field: 'ModificationTime', title: '修改时间', align: "left", width: '150px',
                    formatter: function (value, row, index) {
                        return ys.formatDate(value, "yyyy-MM-dd HH:mm:ss");
                    }
                },
                { field: 'Id', title: 'Id', align: "left", width:'150px'}
            ],
            onLoadSuccess: function () {
                if (id != '') {
                    $('#gridTable').ysTreeTable('expandRowById', id);
                }
            }
        };
        $('#gridTable').ysTreeTable(options);
    }

    function searchTreeGrid(callBackId) {
        var param = $("#searchDiv").getWebControls();
        $('#gridTable').ysTreeTable('search', param);
        if (callBackId) {
            id = callBackId;
        }
    }

    function showAddTopDialog() {
        var parentId = '@defId';
        ys.openDialog({
            title: "创建" + typeName,
            height: '400px',
            content: '@Url.Content("~/Scc/" + controller + "/AddTopModal")' + '?type=' + type + '&id=' + '' + '&parentId=' + parentId ,
            callback: function (index, layero) {
                var frame = window[layero.find('iframe')[0]['name']];
                frame.saveForm(index);
            }
        });
    }

    function showAddEditDialog(add) {
        var parentId = '';
        var selectedRows = $("#gridTable").bootstrapTreeTable("getSelections");
        if (add) {
            id = '';
            if (selectedRows.length > 0) {
                var id1= selectedRows[0].Id;
                var row = $('#gridTable').ysTreeTable('getRowById', id1);
                if (row) {
                    parentId = row.Id;
                    parentName = row.Name;
                }
            }
            else {
                ys.msgError("请选择上级" + typeName);
                return;
            }
        }
        else {
            if (!ys.checkRowEdit(selectedRows)) {
                return;
            }
            id = selectedRows[0].Id;
            parentId = selectedRows[0].ParentId;
        }

        ys.openDialog({
            title: id == '' ? "新增" + typeName : "修改" + typeName,
            height: '400px',
            content: '@Url.Content("~/Scc/" + controller + "/AddEditModal")' + '?type=' + type + '&id=' + id + '&parentId=' + parentId,
            callback: function (index, layero) {
                var frame = window[layero.find('iframe')[0]['name']];
                frame.saveForm(index);
            }
        });
    }

    function deleteSelected() {
        var selectedRows = $("#gridTable").bootstrapTreeTable("getSelections");
        if (ys.checkRowDelete(selectedRows)) {
            ys.confirm("确认要删除选中的" + selectedRows.length + "条数据吗？", function () {
                var ids = ys.getIds(selectedRows);
                var id1 = selectedRows[0].Id;
                var pId1 = selectedRows[0].ParentId;
                ys.ajax({
                    url: '@Url.Content("~/Scc/" + controller + "/DeleteSelected")' + '?type=' + type + '&id=' + id1,
                    type: "post",
                    error: ys.ajaxError,
                    success: function (rst) {
                        if (rst.Flag == 1) {
                            ys.msgSuccess(rst.Message);
                            searchTreeGrid(pId1);
                        }
                        else ys.msgError(rst.Message);
                    }
                });
            });
        }
    }
</script>